def b26e(n):
    OrdA=ord('A')
    n=(n ^ 0x45)+7
    res=[]
    while n>0:
        n,r=divmod(n,26)
        res.append(chr(OrdA+r))
    return ''.join(res)

def b26d(s):
    OrdA=ord('A')
    r=0
    for x in reversed(s):
        r=r*26+ord(x)-OrdA
    return (r-7)^0x45

import time
def mysleep(): time.sleep(0.1)

class JIter(object):
    def __init__(self,g,sleepfunc=mysleep):
        self.g=g
        self.ref=0
        self.cnt=0
        self.pos=0
        self.sleepfunc=sleepfunc

    def Skip(self):
        self.ref,_=self.g.ScanJournal(self.ref)
        self.cnt=self.pos=0

    def __iter__(self):
        return self

    def next(self,wait=True):
        while self.pos>=self.cnt:
            self.ref,self.cnt=self.g.ScanJournal(self.ref)
            self.pos=0
            if not wait:
                return '',0
            self.mysleep()
        self.pos+=1
        return self.g.GetJournal(self.pos-1)

    def WaitEvent(self,msgs=None,tout=None,targ=False):
        s=time.time()
        while True:
            msg=self.next(wait=False)
            if msgs:
                for x in msgs:
                    if x in msg[0]:
                        return msg[0]
            if targ and self.g.TargCurs:
                return 'TARG'
            if (tout is not None) and (time.time()>s+tout):
                return 'TIMEOUT'
